<template>
  <view class="search_result_box">
    <u-tabs
      v-if="statusList && statusList.length > 0"
      :list="statusList"
      @click="click"
      keyName="title"
      :scrollable="false"
      :current="current"
    ></u-tabs>
    <u-search
      v-if="query.title"
      placeholder="请输入"
      v-model="searchName"
      showAction
      @custom="custom"
    ></u-search>
    <view
      class="search_result_item"
      v-for="(item, index) in dataList"
      :key="index"
    >
      <view class="search_result_val">
        <view class="font-34 fontW-7">{{ item.name }}</view>
        <view class="search_result_tips">
          <view>
            {{ item.gysName }} -
            {{
              statusArr.find((obj) => item.status === obj.id)
                ? statusArr.find((obj) => item.status === obj.id).label
                : "正常"
            }}
          </view>
          <!-- <view v-if="jumpFlag(item) !== 1">{{ item.updateTime }}</view> -->
          <view>{{ showTime(item) }}</view>
        </view>
      </view>
      <button
        class="login-btn cu-btn block bg-blue lg round"
        @click="jump(jumpUrl(item))"
      >
        {{ jumpFlag(item) == 1 ? query.title : "详 情" }}
      </button>
    </view>
    <view v-if="dataList.length === 0"><u-empty mode="list"> </u-empty></view>
    <view></view>
    <view v-if="getLoading" class="loading_box flex">
      <u-loadmore
        :status="status"
        loading-text="努力加载中...."
        nomoreText="没有更多了"
      />
    </view>
  </view>
</template>
    
<script>
import { getByPage, getByPageOne } from "@/api/system/searchResult.js";
import { mapState } from "vuex";
export default {
  data() {
    return {
      getLoading: false,
      status: "loading", // nomore   loading
      query: {},
      pageNumber: 1,
      pageSize: 10,
      dataList: [],
      searchName: "",
      timeFormat: uni.$u.timeFormat,
      statusList: [],
      current: 0,
    };
  },
  computed: {
    ...mapState("search", ["filterObj"]),
    ...mapState("user", ["statusArr"]),
  },
  onLoad: function (option) {
    this.query = option || {};
    uni.setNavigationBarTitle({ title: option.title });
  },
  onShow() {
    this.reset();
    this.getByPageFn();
  },
  onPullDownRefresh() {
    this.reset();
    this.getByPageFn(1);
  },
  onReachBottom() {
    console.log(123456);
    if (this.status === "nomore") return;
    setTimeout(() => {
      this.pageNumber++;
      this.getByPageFn();
    }, 400);
  },
  methods: {
    showTime(item) {
      const { status, nextHealthTime, processTime, repairTime } = item;
      if ([101, 103].includes(status)) {
        return nextHealthTime;
      } else if ([102, 202, 302].includes(status)) {
        return processTime;
      } else if ([201, 301].includes(status)) {
        return repairTime;
      } else {
        return "";
      }
    },
    reset(falg) {
      this.dataList = [];
      this.getLoading = true;
      if (!falg) this.searchName = "";
      this.status = "loading";
      this.pageNumber = 1;
    },
    click(e) {
      this.current = e.index;
      this.reset();
      this.getByPageFn();
    },
    jumpFlag(obj) {
      let { repairType, type } = this.query;
      if (this.query.title) {
        if (repairType == 1 && (obj.status === 101 || obj.status === 103)) {
          return 1;
        } else if (repairType == 2 && obj.status !== 201) {
          return 1;
        } else if (repairType == 3 && obj.status === 201) {
          return 1;
        } else if (repairType == 4 && obj.status !== 301) {
          return 1;
        } else if (repairType == 5 && obj.status === 301) {
          return 1;
        } else {
          return 2;
        }
      } else {
        return 3;
      }
    },
    jump(url) {
      this.$tab.navigateTo(url);
    },
    jumpUrl(item) {
      let num = this.jumpFlag(item);
      if (num == 1) {
        return `/pages/reportForRepair/index?id=${item.id}&type=${this.query.type}&title=${this.query.title}&repairType=${this.query.repairType}`;
      } else if (num == 2) {
        return `/pages/equipmentDetail/index?id=${item.id}&showOperation=1&repairType=${this.query.repairType}`;
      } else {
        return `/pages/equipmentDetail/index?id=${item.id}`;
      }
    },
    custom() {
      this.reset(true);
      this.getByPageFn();
    },
    getByPageFn(num) {
      let param;
      if (this.query.title) {
        // 保养等页面
        param = {
          condition: this.searchName,
          type: this.query.type,
          repairType: this.query.repairType,
          pageNumber: this.pageNumber,
          pageSize: this.pageSize,
          status: this.statusList[this.current]?.value ?? undefined,
        };
        getByPageOne(param).then((res) => {
          let arr = res?.result?.data?.records || [];
          this.statusList = res?.result?.statusList || [];
          if (arr.length < 10) this.status = "nomore";
          if (num) uni.stopPullDownRefresh();
          this.dataList.push(...arr);
        });
      } else {
        // 搜索
        param = {
          wareStareTime:
            (this.filterObj?.startTime &&
              this.timeFormat(this.filterObj?.startTime, "yyyy-mm-dd")) ||
            undefined,
          wareEndTime:
            (this.filterObj?.endTime &&
              this.timeFormat(this.filterObj?.endTime, "yyyy-mm-dd")) ||
            undefined,
          condition: this.filterObj?.name,
          gysName:
            (this.filterObj?.brand?.id && this.filterObj?.brand?.name) ||
            undefined,
          xh:
            (this.filterObj?.modelNum?.id && this.filterObj?.modelNum?.value) ||
            undefined,
          warehouse:
            (this.filterObj?.warehouse?.id &&
              this.filterObj?.warehouse?.name) ||
            undefined,
          pageNumber: this.pageNumber,
          pageSize: this.pageSize,
        };
        getByPage(param).then((res) => {
          let arr = res?.result?.records || [];
          if (arr.length < 10) this.status = "nomore";
          if (num) uni.stopPullDownRefresh();
          this.dataList.push(...arr);
        });
      }
    },
  },
};
</script>
    
<style lang="scss">
page {
  background-color: #ffffff;
}
.search_result_box {
  padding: 30upx;
  .search_result_item {
    display: flex;
    padding: 30upx 0;
    border-bottom: 1px solid #eee;
    .search_result_val {
      flex: 1;
      .search_result_tips {
        margin-top: 20upx;
      }
    }
    .jump_detail {
      background: #4896f8;
      padding: 16upx 30upx;
      color: #fff;
      border-radius: 8upx;
    }
  }
  .loading_box {
    height: 100upx;
    line-height: 100upx;
  }
}
</style>
    